﻿Imports System
Imports System.Text.RegularExpressions


Public NotInheritable Class WordCount
    Private Sub New()
    End Sub
    ''' <summary>
    ''' Count words with Regex.
    ''' </summary>
    Public Shared Function CountWords1(s As String) As Integer
        Dim collection As MatchCollection = Regex.Matches(s, "[\S]+")
        Return collection.Count
    End Function

    ''' <summary>
    ''' Count word with loop and character tests.
    ''' </summary>
    Public Shared Function CountWords2(s As String) As Integer
        Dim c As Integer = 0
        For i As Integer = 1 To s.Length - 1
            If Char.IsWhiteSpace(s(i - 1)) = True Then
                If Char.IsLetterOrDigit(s(i)) = True OrElse Char.IsPunctuation(s(i)) Then
                    c += 1
                End If
            End If
        Next
        If s.Length > 2 Then
            c += 1
        End If
        Return c
    End Function
End Class
